import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from '@/components/bs-ui/select'
import { userContext } from '@/contexts/userContext'
import { getAssistantModelsApi } from '@/controllers/API/assistant'
import { useContext, useEffect, useState } from 'react'

export default function ModelSelect({ value, onChange }) {
  const { user } = useContext(userContext)
  const [configServers, setConfigServers] = useState([])
  const loadModels = async () => {
    const data = await getAssistantModelsApi(user.enterprise_id)
    setConfigServers(data)
  }

  useEffect(() => {
    loadModels()
  }, [])

  return (
    <Select name="model" required value={value} onValueChange={onChange}>
      <SelectTrigger className="mt-2">
        <SelectValue placeholder="选择一个模型"></SelectValue>
      </SelectTrigger>
      <SelectContent>
        <SelectGroup>
          {configServers.map(server => (
            <SelectItem key={server.id} value={server.model_name}>
              {server.model_name}
            </SelectItem>
          ))}
        </SelectGroup>
      </SelectContent>
    </Select>
  )
}
